<template>
  <el-dialog
    :close-on-click-modal="false"
    title="仓库设置"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="仓库编号" prop="number">
            <el-input v-model="form.number" :disabled="true" placeholder="自动生成" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库名称" prop="name">
            <el-input v-model="form.name" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓库描述" prop="description">
            <el-input v-model="form.description" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="机构名称" prop="organizationName">
            <el-input v-model="form.organizationName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="form.contactPerson" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8">
          <el-form-item label="是否启用" prop="enable">
            <el-input v-model="form.enable" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="编制时间" prop="createTime">
            <el-date-picker v-model="form.createTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addStorehouseInfo, updateStorehouseInfo, getStorehouseInfoDetail } from '@/api/materialManage'
import { getAreaList } from '@/api/common'

export default {
  name: 'AddStorehouse',
  components: {
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        number: '',
        name: '',
        description: '',
        organizationName: '',
        contactPerson: '',
        contactPhone: '',
        // enable: '',
        createTime: ''
      },
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    // data: {
    //   handler(newData, oldData) {
    //     if (newData.id) {
    //       this.getDocDetail(newData.id)
    //       Object.keys(this.form).forEach(key => {
    //         const source = newData[key]
    //         if (typeof source === 'boolean') {
    //           this.form[key] = source ? 1 : 0
    //         } else {
    //           this.form[key] = source
    //         }
    //       })
    //     }
    //   },
    //   deep: true,
    //   immediate: true
    // },
    showModal(val) {
      if (val) {
        if (this.optType === 'edit') {
          this.getDocDetail(this.data.id)
          this.$nextTick(() => {
            Object.keys(this.form).forEach(key => {
              const source = this.data[key]
              if (typeof source === 'boolean') {
                this.form[key] = source ? 1 : 0
              } else {
                this.form[key] = source
              }
            })
          })
        } else if (this.optType === 'add') {
          const userInfo = this.$store.state.user.userInfo
          this.form.organizationName = userInfo.organizationName
        }
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getStorehouseInfoDetail({ storehouseId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            ...this.form,
            ...this.optType === 'edit' ? {
              id: this.data.id
            } : {}
          }
          const fn = this.optType === 'add' ? addStorehouseInfo : updateStorehouseInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
